package com.tools.cleanmaster.store;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.tools.cleanmaster.common.thread.ThreadPool;
import com.tools.cleanmaster.utils.DateTimeUtils;
import com.tools.cleanmaster.utils.LogHelper;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Li JingYi on 16/6/30.
 */
public class SplashSharedPrefManager {

    private static final String TAG = "SplashSharedPrefManager";

    private static final String CATEGORY_SPLASH_AD = "13246";

    private static final String JSON_SPLASH_SHOW_TIME = "splash_show_time";
    private static final String JSON_ORGANIC_MAX_SHOW_COUNT = "organic_ad_count";
    private static final String JSON_NOT_ORGANIC_MAX_SHOW_COUNT = "no_organic_ad_count";
    private static final String JSON_ORGANIC_PROTECT_TIME = "organic_new_protect_time";
    private static final String JSON_NOT_ORGANIC_PROTECT_TIME = "no_organic_new_protect_time";
    private static final String JSON_SP_NATIVE_SHOW_TIME = "sp_native_ad_show_time";
    public static final long DELAY_TIME_DEFAULT = 1000;
    private static final int DEFAULT_ORGANIC_AD_COUNT = 0;
    private static final int DEFAULT_NO_ORGANIC_AD_COUNT = 0;
    private static final int DEFAULT_ORGANIC_PROTECT_TIME = 6;
    private static final int DEFAULT_NO_ORGANIC_PROTECT_TIME = 6;
    private static final String SPLASH_SHARED_FILE = "splash_config_file";
    private static final String KEY_LAST_DISPLAYED_DAY = "key_last_display_day";
    private static final String KEY_DISPLAY_COUNT = "key_display_count";

    private static SharedPreferences sPrefs = null;

    private static SharedPreferences getSharedPreferences(Context context) {
        if (sPrefs == null) {
            sPrefs = context.getSharedPreferences(SPLASH_SHARED_FILE, Context.MODE_PRIVATE);
        }
        return sPrefs;
    }

    public static void initDataPipe(final Context context) {
        /*NotifyManager.registDataPipe(CATEGORY_SPLASH_AD, new NotifyManager.IDataPipeCallback() {
            @Override
            public void onDataArrive(String category, String body) {
                if (TextUtils.isEmpty(body)) {
                    LogHelper.i(TAG, "数据通道拉取成功" + " 数据通道ID=" + category + " 数据配置为空");
                    return;
                }
                LogHelper.i(TAG, "数据通道拉取成功" + " 数据通道ID=" + category + " body=" + body);
                storeData(category, body, context);
            }
        });*/

        ThreadPool.runOnPool(new Runnable() {
            @Override
            public void run() {
                LogHelper.i(TAG, "数据通道初始化");
                //storeData(CATEGORY_SPLASH_AD, NotifyManager.getDataPipeData(CATEGORY_SPLASH_AD), context);
            }
        });
    }

    private static void storeData(String categoryId, String pipeData, Context context) {
        try {
            if (TextUtils.isEmpty(pipeData)) {
                LogHelper.i(TAG, "PipeData is Empty");
                return;
            }

            if (CATEGORY_SPLASH_AD.equals(categoryId)) {
                JSONObject jsonObject = new JSONObject(pipeData);
                if (jsonObject.has(JSON_SPLASH_SHOW_TIME)) {
                    saveSplashDelay(jsonObject.optLong(JSON_SPLASH_SHOW_TIME, DELAY_TIME_DEFAULT), context);
                }
                if (jsonObject.has(JSON_NOT_ORGANIC_MAX_SHOW_COUNT)) {
                    saveNotOrganicShowCount(context, jsonObject.optInt(JSON_NOT_ORGANIC_MAX_SHOW_COUNT, DEFAULT_NO_ORGANIC_AD_COUNT));

                }
                if (jsonObject.has(JSON_ORGANIC_MAX_SHOW_COUNT)) {
                    saveOrganicShowCount(context, jsonObject.optInt(JSON_ORGANIC_MAX_SHOW_COUNT, DEFAULT_ORGANIC_AD_COUNT));
                }
                if (jsonObject.has(JSON_NOT_ORGANIC_PROTECT_TIME)) {
                    saveNotOrganicProtectTime(context, jsonObject.optInt(JSON_NOT_ORGANIC_PROTECT_TIME, DEFAULT_NO_ORGANIC_PROTECT_TIME));
                }
                if (jsonObject.has(JSON_ORGANIC_PROTECT_TIME)) {
                    saveOrganicProtectTime(context, jsonObject.optInt(JSON_ORGANIC_PROTECT_TIME, DEFAULT_ORGANIC_PROTECT_TIME));
                }
                if (jsonObject.has(JSON_SP_NATIVE_SHOW_TIME)) {
                    saveSpNativeShowTime(context, jsonObject.optLong(JSON_SP_NATIVE_SHOW_TIME, 10000l));
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
            LogHelper.i(TAG, "JSON Parsing Error! JSON: " + pipeData);
        }
    }

    private static void saveSplashDelay(long timeout, Context context) {
        getSharedPreferences(context).edit().putLong(JSON_SPLASH_SHOW_TIME, timeout).apply();
    }

    public static long getSplashDelay(Context context) {
        return getSharedPreferences(context).getLong(JSON_SPLASH_SHOW_TIME, DELAY_TIME_DEFAULT);
    }

    private static void saveNotOrganicShowCount(Context context, int count) {
        getSharedPreferences(context).edit().putInt(JSON_NOT_ORGANIC_MAX_SHOW_COUNT, count).apply();
    }

    private static void saveOrganicShowCount(Context context, int count) {
        getSharedPreferences(context).edit().putInt(JSON_ORGANIC_MAX_SHOW_COUNT, count).apply();
    }

    private static void saveNotOrganicProtectTime(Context context, int protectTime) {
        getSharedPreferences(context).edit().putInt(JSON_NOT_ORGANIC_PROTECT_TIME, protectTime).apply();
    }

    public static int getMaxShowCount(Context context) {
        String showCountKey = GlobalSharedPrefManager.isOrganic(context) ? JSON_ORGANIC_MAX_SHOW_COUNT : JSON_NOT_ORGANIC_MAX_SHOW_COUNT;
        int defaultShowCount = GlobalSharedPrefManager.isOrganic(context) ? DEFAULT_ORGANIC_AD_COUNT : DEFAULT_NO_ORGANIC_AD_COUNT;
        return getSharedPreferences(context).getInt(showCountKey, defaultShowCount);
    }

    private static void saveOrganicProtectTime(Context context, int protectTime) {
        getSharedPreferences(context).edit().putInt(JSON_ORGANIC_PROTECT_TIME, protectTime).apply();
    }

    public static int getNewProtectTime(Context context) {
        String protectTimeKey = GlobalSharedPrefManager.isOrganic(context) ? JSON_ORGANIC_PROTECT_TIME : JSON_NOT_ORGANIC_PROTECT_TIME;
        int defaultProtectTime = GlobalSharedPrefManager.isOrganic(context) ? DEFAULT_ORGANIC_PROTECT_TIME : DEFAULT_NO_ORGANIC_PROTECT_TIME;
        return getSharedPreferences(context).getInt(protectTimeKey, defaultProtectTime);
    }

    private static void saveSpNativeShowTime(Context context, long showTime) {
        getSharedPreferences(context).edit().putLong(JSON_SP_NATIVE_SHOW_TIME, showTime).apply();
    }

    public static long getSpNativeShowTime(Context context) {
        return getSharedPreferences(context).getLong(JSON_SP_NATIVE_SHOW_TIME, 1000);
    }

    public static void setLastDisplayedDay(Context context) {
        getSharedPreferences(context).edit().putString(KEY_LAST_DISPLAYED_DAY, currentDay()).apply();
    }

    private static String getLastDisplayDay(Context context) {
        return getSharedPreferences(context).getString(KEY_LAST_DISPLAYED_DAY, "");
    }

    public static String currentDay() {
        long time = System.currentTimeMillis();
        return DateTimeUtils.formatDate(time);
    }

    public static void addShowCount(Context context) {
        getSharedPreferences(context).edit().putInt(KEY_DISPLAY_COUNT, getShowCount(context) + 1).apply();
    }

    private static void resetShowCount(Context context) {
        getSharedPreferences(context).edit().putInt(KEY_DISPLAY_COUNT, 0).apply();

    }

    private static int getShowCount(Context context) {
        return getSharedPreferences(context).getInt(KEY_DISPLAY_COUNT, 0);
    }

    public static int getCanShowCount(Context context) {
        if (currentDay().equals(getLastDisplayDay(context))) {
            return getMaxShowCount(context) - getShowCount(context);
        } else {
            resetShowCount(context);
            return getMaxShowCount(context);
        }
    }
}
